package org.javaforever.cookieshop.controller;

import java.util.List;

import org.javaforever.cookieshop.domain.GoodsBean;
import org.javaforever.cookieshop.domain.TypeBean;
import org.javaforever.cookieshop.service.GoodsService;
import org.javaforever.cookieshop.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class IndexController { 
	@Autowired
	protected GoodsService goodsService;
	
	@Autowired
	protected TypeService typeService;
    
    @GetMapping("/index2")
    public String index2(Model model){
        //参数1：将要用到jsp文件中的名字
        //参数2：查询到的内容或对象
        model.addAttribute( "msg","Hello world,springboot + jsp" );
        return "index2";
    }
    
    @GetMapping("/mygoodslist")
    public String mygoodslist(Model model) throws Exception{
    	System.out.println("counts:"+goodsService.listAllGoodses().size());
    	model.addAttribute("counts", goodsService.listAllGoodses().size());
    	model.addAttribute("msg", "Hello, world!");
    	model.addAttribute("goodsData", goodsService.listAllGoodses());
        return "mygoodslist";
    }
    
    @GetMapping({"/index","/"})
    public String index(Model model) throws Exception{
		List<GoodsBean> ScrollGood=goodsService.getScrollGood();
		model.addAttribute("scroll",ScrollGood);
	
	    List<GoodsBean>newList=goodsService.getGoodsList(3);
	    model.addAttribute("newList",newList);
	
	    List<GoodsBean>hotList=goodsService.getGoodsList(2);
	    model.addAttribute("hotList",hotList);
	    
	    List<TypeBean>typeList= typeService.listActiveTypes();
	    model.addAttribute("typeList",typeList);	
	    return "index";
    }
}
